package com.tledu.zyf.listener;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextLoaderListener implements ServletContextListener {

	/**
	 * 创建对象时执行
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("ServletContext对象被创建了");

		// 1 获取ServletContext对象
		ServletContext servletContext = sce.getServletContext();
		// 2 加载资源文件
		String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");

		// 3 获取真实路径
		String realPath = servletContext.getRealPath(contextConfigLocation);
		System.out.println(realPath);
		// 4 载入内存
		try {
			FileInputStream fis = new FileInputStream(realPath);
			System.out.println(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 销毁对象时 执行
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("ServletContext对象被销毁了");
	}

}
